﻿#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Utilities.Testing;
using System.IO;
using Newtonsoft.Utilities.IO;
using NUnit.Framework;

namespace Newtonsoft.Utilities.Tests.IO
{
  public class StreamExtensionsTests : TestsBase
  {
    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: stream")]
    public void ReadAllText_ThrowsExceptionForNullStream()
    {
      MemoryStream ms = null;
      ms.ReadAllText();
    }

    [Test]
    public void ReadAllText_ReadUTF8Text()
    {
      string initialText = "Test text";
      byte[] data = Encoding.UTF8.GetBytes(initialText);
      MemoryStream ms = new MemoryStream(data);

      Assert.AreEqual(initialText, ms.ReadAllText());
    }

    [Test]
    public void ReadAllText_ReadBigEndianUnicodeText()
    {
      string initialText = "Test text";
      byte[] data = Encoding.BigEndianUnicode.GetBytes(initialText);
      MemoryStream ms = new MemoryStream(data);

      Assert.AreEqual("\0T\0e\0s\0t\0 \0t\0e\0x\0t", ms.ReadAllText());

      ms = new MemoryStream(data);
      ms.Seek(0, SeekOrigin.Begin);
      Assert.AreEqual(initialText, ms.ReadAllText(Encoding.BigEndianUnicode));
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: sourceStream")]
    public void WriteToStream_ThrowsExceptionForNullSource()
    {
      MemoryStream ms = null;
      ms.WriteToStream(new MemoryStream());
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: destinationStream")]
    public void WriteToStream_ThrowsExceptionForNullDestination()
    {
      MemoryStream ms = new MemoryStream();
      ms.WriteToStream(null);
    }

    [Test]
    public void WriteToStream_ReadBigEndianUnicodeText()
    {
      string initialText = "Test text";
      byte[] data = Encoding.UTF8.GetBytes(initialText);
      MemoryStream source = new MemoryStream(data);

      MemoryStream destination = new MemoryStream();
      source.WriteToStream(destination, 1024);

      destination.Seek(0, SeekOrigin.Begin);
      Assert.AreEqual("Test text", destination.ReadAllText());
    }
  }
}
